// pages/member-friendDynamic/member-friendDynamic.js
import api from '../../utils/api.js'
import { config } from '../../utils/config'
let app = getApp();
import {
    HTTP
} from '../../utils/http.js'
let http = new HTTP()
Page({
    data: {
        // 此页面 页面内容距最顶部的距离
        height: app.globalData.height * 2 + 20,
        friendDynamicList: [], //动态列表
        releaseFocus: false, //回复键盘
        imgUrl: config.imgUrl,
        replyContent: ''
    },
    // 点击回复 ，自动聚焦，拉起键盘
    bindReply: function(e) {
        let parent_id = e.currentTarget.dataset.parent_id;
        let item_index = e.currentTarget.dataset.item_index;
        console.log('show+++++++++++')
        this.setData({
            releaseFocus: true,
            parent_id,
            item_index,
        })
    },
    //获得自己朋友圈更新列表
    getCronyDynamicList(user_id) {
        http.request({
            url: api.getCronyDynamicList,
            data: {
                id: user_id,
                page: 1,
                size: 10
            },
            success: (res) => {
                console.log('朋友圈', res);
                if (res.code == 200) {
                    res.data.forEach((ele) => {
                        ele.stretchSign = false
                    })

                    res.data.map((ele, index, arr) => {
                        this.sureCronyAttention(ele.id, user_id, arr)
                    })
                    console.log('111111', res.data)
                        // this.setData({
                        //   friendDynamicList: res.data
                        // })
                } else if (res.error_code == 201) {
                    let msg = '您的朋友圈还没有动态，快去发布一个吧！'
                    this.setData({
                        friendDynamicNone: msg
                    })
                }
            }
        });
    },
    onLoad: function(options) {

    },
    //判断用户是否点赞一条留言
    sureCronyAttention(id, uid, arr) {
        http.request({
            url: api.sureCronyAttention,
            data: {
                id: id, //朋友圈ID
                uid: uid, //用户id
            },
            success: (res) => {
                console.log('判断用户是否点赞一条留言', res);
                if (res.code == 200) {
                    arr.forEach((ele) => {
                        if (ele.id == id) {
                            ele.like = true;
                        }
                    })
                    this.setData({
                        friendDynamicList: arr
                    })
                } else if (res.code == 201) {
                    arr.forEach((ele) => {
                        if (ele.id == id) {
                            ele.like = false;
                        }
                    })
                    this.setData({
                        friendDynamicList: arr
                    })
                }
            }
        });
    },
    //用户点赞或者取消点赞一条帖子
    //statusID————1点赞  2取消点赞
    addCronyAttention(e) {
        let friendDynamicList = this.data.friendDynamicList;
        let id = e.currentTarget.dataset.id;
        let uid = wx.getStorageSync('user_id');
        let statusID = e.currentTarget.dataset.statusid;
        http.request({
            url: api.addCronyAttention,
            data: {
                id, //帖子ID
                uid, //用户id
                statusID
            },
            success: (res) => {
                if (res.code == 200 && statusID == 1) {
                    console.log('朋友圈——用户点赞成功', res);
                    friendDynamicList.forEach((ele) => {
                        if (ele.id === id) {
                            ele.like = true
                            ele.attention += 1
                        }
                    })
                    this.setData({
                        friendDynamicList
                    })
                } else if (res.code == 200 && statusID == 2) {
                    console.log('朋友圈——用户取消点赞点赞成功', res);
                    friendDynamicList.forEach((ele) => {
                        if (ele.id === id) {
                            ele.like = false
                            ele.attention -= 1
                        }
                    })
                    this.setData({
                        friendDynamicList
                    })
                }
            }
        });
    },
    onShow: function() {
        let user_id = wx.getStorageSync('user_id')
        this.getCronyDynamicList(user_id)
    },
    //去往——发布动态 msgType: 1 友邻吧  2 朋友圈
    goSendMsg() {
        wx.navigateTo({
            url: `/pages/sendMsg/sendMsg?msgType=2`,
        })
    },


    //隐藏输入框, 失去聚焦
    blur: function(e) {
        let value = e.detail.value.replace(/\s+/g, '');
        this.setData({
            releaseFocus: false,
            replyContent: value
        })
        console.log('hide+++++++++++')
    },
    //输入框 的 内容
    bindInputMsg(e) {
        console.log('11111111', e)
        let value = e.detail.value.replace(/\s+/g, '');
        this.setData({
            replyContent: value
        })
    },
    //输入框自带的————回复帖子，点击完成按钮时触发，
    sendTextMsg(e) {
        console.log('eeeeeeeeee', e)
        let replyContent = e.detail.value.replace(/\s+/g, '');
        let parent_id = this.data.parent_id;
        let item_index = this.data.item_index;
        let user_id = wx.getStorageSync('user_id');

        if (replyContent.length == 0) {
            wx.showToast({
                title: '不能回复空哦！',
                icon: 'none'
            })
        } else {
            let data = {
                user_id: user_id,
                parent_id: parent_id, //父帖子ID
                content: replyContent,
                images: []
            }
            this.addCronyDynamicOne(data, item_index);
        }
    },
    // 自定义的发送
    sendTextMsgMy() {
        let replyContent = this.data.replyContent;
        let parent_id = this.data.parent_id;
        let item_index = this.data.item_index;
        let user_id = wx.getStorageSync('user_id');

        console.log('replyContent', replyContent)
        if (replyContent.length == 0) {
            wx.showToast({
                title: '不能回复空哦！',
                icon: 'none'
            })
        } else {
            let data = {
                user_id: user_id,
                parent_id: parent_id, //父帖子ID
                content: replyContent,
                images: []
            }
            this.addCronyDynamicOne(data, item_index);
        }

    },
    //调用接口，传入父帖子id——parent_id
    addCronyDynamicOne(data, item_index) {
        http.request({
            url: api.addCronyDynamicOne,
            data: data,
            success: (res) => {
                console.log('朋友圈-动态回复成功', res);
                if (res.code == 200) {

                    this.partRefreshData(item_index, data.parent_id, data.content)
                    wx.showToast({
                        title: '回复成功',
                        mask: true,
                        icon: 'success',
                        duration: 1500,
                    })
                } else if (res.code == 201) {

                }

            }
        });
    },
    //更多回复，收缩展开
    clickStretch(e) {
        let index = e.currentTarget.dataset.index;
        console.log('index', index)
        let friendDynamicList = this.data.friendDynamicList;
        let stretch_sign = friendDynamicList[index].stretchSign;
        var item = "friendDynamicList[" + index + "].stretchSign";

        this.setData({
            [item]: !stretch_sign
        })

    },
    //回复 成功，局部刷新数据
    partRefreshData(index, parent_id, content) {
        console.log('index', index)
        console.log('parent_id', parent_id)
        console.log('content', content)

        let friendDynamicList = this.data.friendDynamicList;
        let nickname = wx.getStorageSync('nickName');
        friendDynamicList[index].parent_id.push({
            parent_id: parent_id,
            content: content,
            users: {
                nickname: nickname
            }
        })
        this.setData({
            friendDynamicList
        })
    },


    onReachBottom: function() {

    }
})